<script type="text/javascript">
var slCtl = null;

//DO NOT FORGET TO REGISTER THIS FUNCTION WITH THE SILVERIGHT CONTROL
// <param name="onload" value="pluginLoaded" />
function pluginLoaded(sender) {
	//IMPORTANT: Make sure this is the same ID as the ID in your <OBJECT tag (<object id="MultiFileUploader" etc)
	slCtl = document.getElementById("MultiFileUploader");

	//Register All Files Finished Uploading event
	slCtl.Content.Files.AllFilesFinished = AllFilesFinished;

	//Register single file finished event
	//slCtl.Content.Files.SingleFileUploadFinished = SingleFileFinished;

	//Register Error occurred during uploading event
	//slCtl.Content.Files.ErrorOccurred = ShowErrorDiv;

	//Set your custom parameter using javascript
	//This parameter will be available in the webservice and you can use it for your business logic
	//Or use it to identity the upload to a sinle row in your database
	slCtl.Content.Files.CustomParams = '<?php echo time(); ?>';
}

//This function is registred in the pluginLoaded function (slCtl.Content.Files.AllFilesFinished = AllFilesFinished;)
function AllFilesFinished() {
	if (slCtl != null) {
		alert('Uploaded ' + slCtl.Content.Files.TotalUploadedFiles + ' files');
	}
	location.href = '<?php echo BASE_URL; ?>file/index/<?php echo $cat_id; ?>';
}

function onSilverlightError(sender, args) {
	var appSource = '';
	if ((sender != null) && (sender != 0)) {
		appSource = sender.getHost().Source;
	}
	var errorType = args.ErrorType;
	var iErrorCode = args.ErrorCode;

	var errMsg = 'Unhandled Error in Silverlight 2 Application ' +  appSource + '\n' ;

	errMsg += 'Code: ' + iErrorCode + '\n';
	errMsg += 'Category: ' + errorType + '\n';
	errMsg += 'Message: ' + args.ErrorMessage + '\n';

	if (errorType == 'ParserError') {
		errMsg += 'File: ' + args.xamlFile + '\n';
		errMsg += 'Line: ' + args.lineNumber + '\n';
		errMsg += 'Position: ' + args.charPosition + '\n';
	} else if (errorType == 'RuntimeError') {
		if (args.lineNumber != 0) {
			errMsg += 'Line: ' + args.lineNumber + '\n';
			errMsg += 'Position: ' + args.charPosition + '\n';
		}
		errMsg += 'MethodName: ' + args.methodName + '\n';
	}

	throw new Error(errMsg);
}
</script>

<?php
$UploadHandlerName = CURRENT_URL;
$MaxFileSizeKB = 2000;
$ChunkSize = 512000; //500KB
$MaxUploads = 1;
$FileFilter = 'Images|*.jpg;*.gif;*.png|All Files (*.*)|*.*';
$DefaultColor = '#fff';

$params  = 'UploadHandlerName=' . $UploadHandlerName . ',';
$params .= 'MaxFileSizeKB='     . $MaxFileSizeKB . ',';
$params .= 'ChunkSize='         . $ChunkSize . ',';
$params .= 'MaxUploads='        . $MaxUploads . ',';
$params .= 'FileFilter='        . $FileFilter . ',';
$params .= 'DefaultColor='      . $DefaultColor;
?>
<!-- Runtime errors from Silverlight will be displayed here.
This will contain debugging information and should be removed or hidden when debugging is completed -->
<div id='errorLocation' style="font-size: small;color: Gray;"></div>

<div id="silverlightControlHost" style="padding:10px;">
	<object id="MultiFileUploader" data="data:application/x-silverlight-2," type="application/x-silverlight-2" Width="450" Height="280">
		<param name="source" value="<?php echo PUBLIC_URL; ?>ext/other/mpost.SilverlightMultiFileUpload.xap"/>
		<param name="onload" value="pluginLoaded" />
		<param name="onerror" value="onSilverlightError" />
		<param name="background" value="#fff" />
		<param name="initParams" value="<?php echo $params; ?>" />
		<param name="minRuntimeVersion" value="4.0.50401.0" />
		<param name="autoUpgrade" value="true" />
		<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50401.0" style="text-decoration:none">
			<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
		</a>
	</object>
</div>